package org.hhchat.leetcode.string;

import java.util.Stack;

/**
 * Created this one by HMH on 2017/11/27.
 */
public class code71_medium {
    static class Solution {
        public String simplifyPath(String path) {
            Stack<String> paths = new Stack<>();
            String[] words = path.split("/");
            for (String word : words) {
                if (word.equals(".")) {
                    continue;
                }
                if (word.equals("..")) {
                    if(!paths.isEmpty())
                        paths.pop();
                }else if (word.length()>0){
                    paths.push(word);
                }
            }
            StringBuilder stringBuilder = new StringBuilder("");
            while (!paths.isEmpty()) {
                stringBuilder.insert(0, paths.pop());
                stringBuilder.insert(0, "/");
            }
            if (stringBuilder.length()==0){
                return "/";
            }
            return stringBuilder.toString();
        }
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.simplifyPath("/../"));
    }
}
